<template>
  <div class="goods">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 ===============================================================-->
    <el-card>
      <!-- 搜索区 ====================================================-->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable>
            <!-- 搜索按钮 -->
            <el-button slot="append" icon="el-icon-search" @click="searchBtn"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addGoodsBtn">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 表格 ====================================-->
      <el-table :data="goodsList" stripe border style="width: 100%">
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称"></el-table-column>
        <el-table-column prop="goods_price" label="价格" width="60"></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="80"></el-table-column>
        <el-table-column prop="add_time" label="创建时间" width="170">
          <template v-slot="scope">{{scope.row.add_time | dateFormat}}</template>
        </el-table-column>
        <el-table-column label="操作" width="130">
          <template v-slot="scope">
            <el-button type="primary" size="mini" icon="el-icon-edit" @click="editBtn"></el-button>
            <el-button
              type="danger"
              size="mini"
              icon="el-icon-delete"
              @click="deleteBtn(scope.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 ====================-->
      <el-pagination
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :page-sizes="[10, 20, 50, 100]"
        :current-page="queryInfo.pagenum"
        :page-size="queryInfo.pagesize"
        :total="total"
        background
      ></el-pagination>
    </el-card>
    <!-- 编辑Dialog -->
    <el-dialog title="编辑" :visible.sync="editDialogVisible" width="50%">
      <el-form
        :model="editGoodsFrom"
        :rules="editGoodsRules"
        ref="editGoodsFormRef"
        label-width="100px"
      >
        <el-form-item label="商品名称:" prop="attr_name">
          <el-input v-model="editGoodsFrom.goods_name"></el-input>
        </el-form-item>
        <el-form-item label="商品价格:" prop="goods_price">
          <el-input v-model="editGoodsFrom.goods_price"></el-input>
        </el-form-item>
        <el-form-item label="商品数量:" prop="goods_number">
          <el-input v-model="editGoodsFrom.goods_number"></el-input>
        </el-form-item>
        <el-form-item label="商品重量:" prop="goods_weight">
          <el-input v-model="editGoodsFrom.goods_weight"></el-input>
        </el-form-item>
      </el-form>
      <!-- 底部按钮 -->
      <span slot="footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editDialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // get请求参数
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      // 商品列表
      goodsList: [],
      // 总数
      total: 0,
      editDialogVisible: false,
      addGoodsDialogVisible: false,
      // 编辑form暂存区
      editGoodsFrom: {
        goods_name: '',
        goods_price: '',
        goods_number: '',
        goods_weight: ''
      },
      // 编辑商品--表单规则
      editGoodsRules: {}
    }
  },
  methods: {
    // 开局获取列表--get请求
    async getGoodsList() {
      const { data: res } = await this.$http.get('goods', {
        params: this.queryInfo
      })
      console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error(`${res.meta.msg}`)
      }
      this.$message.success(`${res.meta.msg}`)
      this.goodsList = res.data.goods
      this.total = res.data.total
      console.log('get请求-商品列表', this.goodsList)
    },
    editBtn() {
      this.editDialogVisible = true
    },
    // 删除按钮
    async deleteBtn(data) {
      console.log(data)
      const confirmResult = await this.$confirm(
        '此操作将永久删除该文件, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(err => err)
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除操作')
      }
      const { data: res } = await this.$http.delete(`goods/${data.goods_id}`)
      if (res.meta.status !== 200) {
        return this.$message.error(`${res.meta.msg}`)
      }
      this.$message.success(`${res.meta.msg}`)
      this.getGoodsList()
    },
    handleSizeChange(newSize) {
      console.log('每页显示:', newSize)
      this.queryInfo.pagesize = newSize
      this.getGoodsList()
    },
    handleCurrentChange(newPage) {
      console.log('当前第几页:', newPage)
      this.queryInfo.pagenum = newPage
      this.getGoodsList()
    },
    // 搜索按钮
    searchBtn() {
      this.getGoodsList()
    },
    addGoodsBtn() {
      console.log('添加商品')
      this.$router.push('/goods/add')
    }
  },
  created() {
    this.getGoodsList()
  }
}
</script>
<style lang="less" scoped>
</style>
